package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.rzjm.api.dao.SmsPhoneDao;
import com.rzjm.api.dao.SmsTemplateDao;
import com.rzjm.api.entity.SmsPhone;
import com.rzjm.api.entity.SmsTemplate;
import com.rzjm.api.service.SmsTemplateService;
import com.rzjm.api.util.SendSmsHW;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SmsTemplateServiceImpl extends BaseServiceImpl<SmsTemplateDao, SmsTemplate> implements SmsTemplateService {

    @Resource
    private SmsTemplateDao templateDao;
    @Resource
    private SmsPhoneDao smsPhoneDao;

    /**
     * 作业审批通知
     * @param userName
     * @param recordType
     */
    @Override
    public void sendSmsByToFireWork(String userName, int recordType) {
        SmsTemplate sms = templateDao.selectOneByColumn("sms_type",0);
        if(sms != null){
//        ${mer}管理员您好，${name}于${time} 领取鸡蛋${count}个，请及时处理。感谢您的使用，祝您生意兴隆~
            SendSmsHW sendSms = new SendSmsHW();
            //请求失败这里会抛ClientException异常
            List<String> phoneNums = new ArrayList<>();
            List<SmsPhone> phones = smsPhoneDao.selectListByColumn("state",1);
            if(CollUtil.isNotEmpty(phones)){
                for (SmsPhone phone : phones){
                    phoneNums.add(phone.getPhoneNum());
                }
            }

            String recordTypeTxt = "";
            if(recordType == 0){
                recordTypeTxt = "动火作业";
            } else if(recordType == 1){
                recordTypeTxt = "密闭作业";
            } else if(recordType == 2){
                recordTypeTxt = "高空作业";
            }
            String templateParas = "[\""+userName+"\",\""+recordTypeTxt+"\"]";
            try {
//                SmsTemplate sms, List<String> phoneNums, String templateParas
//                ${name} 申请开具${zuoye}作业票，请及时确认审批。

                sendSms.sendMsg(sms,phoneNums,templateParas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendSmsByToDianHanji(String userName) {
        SmsTemplate sms = templateDao.selectOneByColumn("sms_type",1);
        if(sms != null){
//        ${mer}管理员您好，${name}于${time} 领取鸡蛋${count}个，请及时处理。感谢您的使用，祝您生意兴隆~
            SendSmsHW sendSms = new SendSmsHW();
            //请求失败这里会抛ClientException异常
            List<String> phoneNums = new ArrayList<>();
            List<SmsPhone> phones = smsPhoneDao.selectListByColumn("state",1);
            if(CollUtil.isNotEmpty(phones)){
                for (SmsPhone phone : phones){
                    phoneNums.add(phone.getPhoneNum());
                }
            }

            String templateParas = "[\""+userName+"\"]";
            try {
//                SmsTemplate sms, List<String> phoneNums, String templateParas
//                ${name} 申请开具${zuoye}作业票，请及时确认审批。

                sendSms.sendMsg(sms,phoneNums,templateParas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
